$(function () {

    $("#card-read .btn-save").click(function () {
        console.log('btn-save')
        var cardNo = $("#cardNo").val();
        var cardState = $("#hi_card_state").val();
        if(cardState != 0){
            $MB.n_danger("卡状态异常，无法做退卡登记！");
            $('#card-read').modal('hide')
            return;
        }
        if (cardNo === null){
            $MB.n_danger("读卡余额为空，请重新操作");
            $('#card-read').modal('hide')
            return;
        }
        var banlance = $("#hi_card_banlance").val();
        if (banlance === null){
            $MB.n_danger("读卡余额为空，请重新操作");
            $('#card-read').modal('hide')
            return;
        }
        var refundamt = $("#refundamt").val();
        if(refundamt === null || refundamt === null){
            $MB.n_danger("退卡金额为空");
            $('#card-read').modal('hide')
            return;
        }
        var deposit = $("#deposit").val().trim();
        if(deposit === null || deposit ===""){
            deposit = 0;
        }
        var par = new Object()
        par.cardNo = cardNo;
        par.cardBanlance = banlance/100;
        par.refundamt = Number(refundamt)+Number(deposit);
        if(par.refundamt === 0){

        }
        par.deposit = deposit;
        console.log(banlance)
        console.log(refundamt)
        console.log(deposit)
        $.post(ctx+"cardInfo/commitRefundCard",par,function (r) {
            if (r.code !== 0) {
                $MB.n_danger(r.msg);
                $('#card-read').modal('hide')
                return;
            }else{
                $MB.n_success("退卡信息已记录！");
                print(r);
                $('#card-read').modal('hide')
                console.log(r)
                return;
            }
        });
    });
    $("#card-read .btn-close").click(function () {
        closeModal();
    });
});

function print(r) {
    var LODOP; //声明为全局变量
    LODOP = getLodop(document.getElementById('LODOP_X'),document.getElementById('LODOP_EM'));
    LODOP.NEWPAGEA();
    //LODOP.ADD_PRINT_RECT(10,18,324,392,0,1);
    LODOP.ADD_PRINT_TEXT(20,15,258,54,"三亚一卡通服务凭证");
    LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑");
    LODOP.SET_PRINT_STYLEA(0,"fontsize",12);

    LODOP.ADD_PRINT_TEXT(55,65,258,54,"退卡完成");
    LODOP.SET_PRINT_STYLEA(0,"fontsize",9);
    LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑");

    LODOP.ADD_PRINT_TEXT(90,3,258,54,"凭证号："+r.data.refundordernum);
    LODOP.SET_PRINT_STYLEA(0,"fontsize",9);
    LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑");

    LODOP.ADD_PRINT_TEXT(120,3,258,54,"卡号："+r.data.cardno);
    LODOP.SET_PRINT_STYLEA(0,"fontsize",9);
    LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑");

    LODOP.ADD_PRINT_TEXT(150,3,258,54,"卡类型："+r.data.cardtypename);
    LODOP.SET_PRINT_STYLEA(0,"fontsize",9);
    LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑");

    LODOP.ADD_PRINT_TEXT(180,3,258,54,"实退卡费："+r.data.deposit/100+"元");
    LODOP.SET_PRINT_STYLEA(0,"fontsize",9);
    LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑")

    LODOP.ADD_PRINT_TEXT(210,3,258,54,"卡余额："+r.data.cardbanlance/100+"元");
    LODOP.SET_PRINT_STYLEA(0,"fontsize",9);
    LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑")

    LODOP.ADD_PRINT_TEXT(240,3,258,54,"实退总金额："+r.data.refundamt/100+"元");
    LODOP.SET_PRINT_STYLEA(0,"fontsize",9);
    LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑")

    LODOP.ADD_PRINT_TEXT(270,3,258,54,"操作人："+r.data.refundusername);
    LODOP.SET_PRINT_STYLEA(0,"fontsize",9);
    LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑")

    LODOP.ADD_PRINT_TEXT(300,3,258,54,"退卡日期："+r.data.refunddate);
    LODOP.SET_PRINT_STYLEA(0,"fontsize",9);
    LODOP.SET_PRINT_STYLEA(0,"FontName","微软雅黑")

    //设定打印页面大小
    LODOP.SET_PRINT_PAGESIZE(3,590,55,"");
    LODOP.SET_PRINT_COPIES(2);
    LODOP.PRINT();
    //LODOP.PREVIEW();
    if (needCLodop()) {
        window.On_CLodop_Opened=function(){
            OpenPreview();
            window.On_CLodop_Opened=null;
        };
    } else
        window.onload = function(){
            OpenPreview();
        };
}

function closeModal() {
    $MB.closeAndRestModal("card-read");
    clearInfo();
}

$('#card-read').on('shown.bs.modal', function () {
    clearInfo();
    card_reading();
})
function clearInfo() {
    $("#paperId").val("");
    $("#cardNo").val("");
    $("#cardUserName").val("");
    $("#cardBanlance").val("");
    $("#cardStateName").val("");
    $("#cardTypeName").val("");
    $("#hi_card_banlance").val("");
    $("#hi_card_state").val("");
    $("#refundamt").val("");
    $("#card-refundamt-amt").text("20");

    //$("#paperPhoto").attr("src","");
}

function amt(){
    var refundamt = $("#refundamt").val();
    var deposit = $("#deposit").val();
    if(refundamt === null || refundamt === ''){
        refundamt = 0
    }
    if(deposit === null || deposit === ''){
        deposit = 0
    }
    var amt = Number(refundamt)+Number(deposit)
    $("#card-refundamt-amt").text(amt);
}

$("#deposit").on("input",function () {
    amt();
})

$("#refundamt").on("input",function () {
    amt();
})

function card_reading() {
    if(SmartCard.SmartCardServer === null){
        $MB.n_danger("读卡器主键启动失败，请重试");
        $('#card-read').modal('hide')
        return;
    }
    var inputReadCardNo="{\"IssueCode\":\"5720FF0000000000\",\"TransCode\":\"10001\"}";
    var outputReadCardNo = jQuery.parseJSON(SmartCard.SmartCardServer(inputReadCardNo));
    if(outputReadCardNo === null){
        $MB.n_danger("读取卡信息错误，请重新操作");
        $('#card-read').modal('hide')
        return;
    }
    if (outputReadCardNo.Result !== 'Success') {
        $MB.n_danger(outputReadCardNo.ErrorInfo);
        $('#card-read').modal('hide')
        return;
    }
    var warningTone="{\"IssueCode\":\"02156410\",\"TransCode\":\"90002\",\"Times\":\"3\",\"Milliseconds\":\"300\"}";
    var warningToneOutPut = jQuery.parseJSON(SmartCard.SmartCardServer(warningTone));
    console.log(warningToneOutPut)
    var cardNo = outputReadCardNo.CardNo;
    console.log(cardNo);
    var obj = new Object();
    obj.cardNo = cardNo;
    $.post(ctx+"cardInfo/getCardInfoByCardNo",obj,function(r){
        if (r.code !== 0) {
            $MB.n_danger(r.msg);
            $('#card-read').modal('hide')
            return;
        }
        console.log(r.cardInfo);
        if(r.cardInfo === null){
            $MB.n_danger("查询不到该卡号信息，请重新操作！");
            $('#card-read').modal('hide')
            return;
        }
        if(r.cardInfo.length === 0){
            $MB.n_danger("查询不到该卡号信息，请重新操作！");
            $('#card-read').modal('hide')
            return;
        }
        $("#paperId").val(r.cardInfo[0].paperId);
        $("#cardNo").val(r.cardInfo[0].cardNo);
        $("#cardUserName").val(r.cardInfo[0].cardUserName);
        $("#cardStateName").val(r.cardInfo[0].cardStateName);
        $("#cardTypeName").val(r.cardInfo[0].cardTypeName);
        $("#hi_card_state").val(r.cardInfo[0].cardState);
        $("#paperPhoto").attr("src","http://120.77.182.149:8080/photos/" + r.cardInfo[0].cardNo.substring(8,16) + ".jpg");
    });
    var InputGetBalance = "{\"IssueCode\":\"5720FF0000000000\",\"TransCode\":\"10002\"}";
    var outputGetBalance = jQuery.parseJSON(SmartCard.SmartCardServer(InputGetBalance));
    if(outputGetBalance === null){
        $MB.n_danger("读取卡余额错误，请重新操作");
        $('#card-read').modal('hide')
        return;
    }
    if(outputGetBalance.Result !== 'Success'){
        $MB.n_danger(outputReadCardNo.ErrorInfo);
        $('#card-read').modal('hide')
        return;
    }
    var banlance = (Number(outputGetBalance.CardBal)/100).toFixed(2);
    $("#cardBanlance").val(banlance);
    $("#refundamt").val(banlance);
    $("#hi_card_banlance").val(outputGetBalance.CardBal);
    amt();
}